package com.zlt.bkparking.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zlt.bkparking.dto.EquipmentDto;
import com.zlt.bkparking.dto.QueryParams;
import com.zlt.bkparking.entity.Equipment;
import com.zlt.bkparking.entity.EquipmentExample;
import com.zlt.bkparking.mapper.EquipmentMapper;
import com.zlt.bkparking.service.EquipmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author: hzj
 * @Description:
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;

    @Override
    public boolean save(EquipmentDto equipment) {
        Equipment entity = new Equipment();
        BeanUtils.copyProperties(equipment,entity);
        return equipmentMapper.insert(entity) > 0;
    }

    @Override
    public boolean remove(Integer id) {

        return equipmentMapper.deleteByPrimaryKey(id) > 0;
    }

    @Override
    public boolean saveOrUpdate(EquipmentDto equipment) {
        Equipment entity = new Equipment();
        BeanUtils.copyProperties(equipment,entity);
        if (Objects.isNull(equipment.geteId())){
            return equipmentMapper.insert(entity) > 0;
        }
        return equipmentMapper.updateByPrimaryKeySelective(entity) > 0;
    }

    @Override
    public Equipment getById(Integer id) {
        return equipmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Equipment> list() {
        EquipmentExample example = new EquipmentExample();
        return equipmentMapper.selectByExample(example);
    }

    @Override
    public PageInfo<Equipment> page(QueryParams params) {
        PageHelper.startPage(params.getPage(),params.getLimit());
        List<Equipment> list = equipmentMapper.selectPage(params.getKey());

        PageInfo<Equipment> pageInfo = new PageInfo<>(list);
        pageInfo.setList(list);
        return pageInfo;
    }

    @Override
    public boolean update(EquipmentDto equipment) {
        Equipment entity = new Equipment();
        BeanUtils.copyProperties(equipment,entity);
        return equipmentMapper.updateByPrimaryKeySelective(entity) > 0;
    }
}
